//
//  ViewController.m
//  WangI
//
//  Created by 刘珊珊 on 15/11/23.
//  Copyright (c) 2015年 刘珊珊. All rights reserved.
//

#import "ViewController.h"
#import "HeadlineModel.h"
#import "NetWorkTask.h"
#import "RootCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *headlineArray;
@property (nonatomic, retain) NSMutableArray *entertainmentArray;
@property (nonatomic, retain) NSMutableArray *sportsArray;
@property (nonatomic, retain) NSMutableArray *technologyArray;
@property (nonatomic, retain) NSMutableArray *businessArray;
@property (nonatomic, retain) UICollectionView *collectView;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) HeadlineModel *headlineModel;
@property (nonatomic, retain) RootCollectionViewCell *root;

@end

@implementation ViewController

-(void)dealloc{
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"网易新闻";
    [self getData];
   
    [self.view addSubview:self.collectView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.scroll];
    
}

-(UIScrollView *)scroll{
    if (!_scroll) {
        self.scroll =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        _scroll.backgroundColor = [UIColor whiteColor];
        _scroll.delegate = self;
        
        NSArray *array = @[@"头条", @"娱乐", @"运动", @"科技", @"财经"];
        for (int i = 0; i < 5; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.tag = 1111 + i;
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitle:array[i] forState:UIControlStateNormal];
            button.frame = CGRectMake(i * (375 / 5), 0, 375 / 5, 30);
            if (i == 0) {
                [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
                button.titleLabel.font = [UIFont systemFontOfSize:17];
            }
            [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:button];
        }
    }
    return _scroll;
}

-(void)scrollViewDidEndDecelerating:(UICollectionView *)scrollView {
    for (int i = 0; i < 5; i++) {
        if (i == scrollView.contentOffset.x / self.view.width) {
            UIButton *bu = (UIButton *)[_scroll viewWithTag:1111 + i];
            [bu setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            bu.titleLabel.font = [UIFont systemFontOfSize:17];
        }else {
            UIButton *bu = (UIButton *)[_scroll viewWithTag:1111 + i];
            [bu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            bu.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}


- (void)buttonDidPress:(UIButton *)button{
    for (NSInteger i = 0; i < 5; i++) {
        if (i == button.tag - 1111) {
            UIButton *bu = (UIButton *)[_scroll viewWithTag:1111 + i];
            [bu setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            bu.titleLabel.font = [UIFont systemFontOfSize:17];
            _collectView.contentOffset = CGPointMake(self.view.width * i, 0);
        }else {
            UIButton *bu = (UIButton *)[_scroll viewWithTag:1111 + i];
            [bu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            bu.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}





-(UICollectionView *)collectView{
    if (!_collectView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        // 滑动方向
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // item的大小
        flowLayout.itemSize = CGSizeMake(self.view.width, self.view.height);
        // item的最小行间距
        flowLayout.minimumLineSpacing = 0;
        
        self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
        self.collectView.backgroundColor = [UIColor whiteColor];
        self.collectView.pagingEnabled = YES;
        _collectView.tag = 1001;
        _collectView.dataSource = self;
        _collectView.delegate = self;
        [_collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"a"];
        [_collectView registerClass:[RootCollectionViewCell class] forCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier"];
    }
    return _collectView;
}

-(NSMutableArray *)headlineArray {
    if (!_headlineArray) {
        self.headlineArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _headlineArray;
}

-(NSMutableArray *)entertainmentArray{
    if (!_entertainmentArray) {
        self.entertainmentArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _entertainmentArray;
}

-(NSMutableArray *)sportsArray{
    if (!_sportsArray) {
        self.sportsArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _sportsArray;
}

-(NSMutableArray *)technologyArray{
    if (!_technologyArray) {
        self.technologyArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _technologyArray;
}

-(NSMutableArray *)businessArray{
    if (!_businessArray) {
        self.businessArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _businessArray;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
        if (indexPath.item == 0) {
         RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier" forIndexPath:indexPath];
        if (self.headlineArray.count > indexPath.item) {
            [cell setArray:self.headlineArray];
        }
            return cell;
        }else if (indexPath.item == 1) {
            RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier" forIndexPath:indexPath];
            cell.array = self.entertainmentArray;
            return cell;
        }else if (indexPath.item == 2) {
            RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier" forIndexPath:indexPath];
            cell.array = self.sportsArray;
            return cell;
        }else if (indexPath.item == 3) {
            RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier" forIndexPath:indexPath];
            cell.array = self.technologyArray;
            return cell;
        }else if (indexPath.item == 4) {
            RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RootCollectionViewCellIdentifier" forIndexPath:indexPath];
            cell.array = self.businessArray;
            return cell;
        }else{
            return cell;
        }
}


/**
 *  获取数据
 */
- (void)getData {
    
//    头条页面
    [NetWorkTask sendRequestWithGet:[NSURL URLWithString:kNewsGETURL] withResult:^(id result) {
        NSArray *array = [[NSArray alloc]initWithArray:[result valueForKey:@"T1348647909107"]];
        for (NSDictionary *dic in array) {
            HeadlineModel *data = [[HeadlineModel alloc]initWithDataSource:dic];
            [self.headlineArray addObject:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
                NSLog(@"%@", data.title);
            });
        }
    }];
    
    
//    娱乐页面
    [NetWorkTask sendRequestWithGet:[NSURL URLWithString:kEntertainmentGETURL] withResult:^(id result) {
        NSArray *array = [[NSArray alloc]initWithArray:[result valueForKey:@"T1348648517839"]];
        for (NSDictionary *dic in array) {
            HeadlineModel *data = [[HeadlineModel alloc]initWithDataSource:dic];
            [self.entertainmentArray addObject:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
            });
        }
    }];
    

//    体育页面
    [NetWorkTask sendRequestWithGet:[NSURL URLWithString:kSportsGETURL] withResult:^(id result) {
        NSArray *array = [[NSArray alloc]initWithArray:[result valueForKey:@"T1348649079062"]];
        for (NSDictionary *dic in array) {
            HeadlineModel *data = [[HeadlineModel alloc]initWithDataSource:dic];
            [self.sportsArray addObject:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
            });
        }
    }];
    
    
//    科技页面
    [NetWorkTask sendRequestWithGet:[NSURL URLWithString:kTechnologyGETURL] withResult:^(id result) {
        NSArray *array = [[NSArray alloc]initWithArray:[result valueForKey:@"T1348649580692"]];
        for (NSDictionary *dic in array) {
            HeadlineModel *data = [[HeadlineModel alloc]initWithDataSource:dic];
            [self.technologyArray addObject:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
            });
        }
    }];
    
    
//    财经页面
    [NetWorkTask sendRequestWithGet:[NSURL URLWithString:kBusinessGETURL] withResult:^(id result) {
        NSArray *array = [[NSArray alloc]initWithArray:[result valueForKey:@"T1348648756099"]];
        for (NSDictionary *dic in array) {
            HeadlineModel *data = [[HeadlineModel alloc]initWithDataSource:dic];
            [self.businessArray addObject:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
            });
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
